package client;

import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;
import org.springframework.context.ApplicationContext;

import service.CustomerManager;
import domain.Customer;

/**
 * Dummy client that just statically call server 's web service configured
 * in a spring application context
 * @author Luc
 *
 */

public class ClientMain {
	
    
    public final static int THREAD_COUNT=10;
    public final static int ITERATION_COUNT=5;
    
    public static CustomerManager customerManager;
    
	public static void main(String[] args) {
			ApplicationContext ctx = new ClassPathXmlApplicationContext("/client/services.xml");
			customerManager = (CustomerManager)ctx.getBean("customerManager");
            
            WsInvoker[] wsInvokers = new WsInvoker[THREAD_COUNT];

            for (int j=0;j<ITERATION_COUNT;j++) {
                for (int i=0;i<THREAD_COUNT;i++) {
                    Customer c = new Customer();
                    c.setId(String.valueOf(i));
                    c.setName("test"+i);
                    wsInvokers[i] = new ClientMain.WsInvoker(c);
                }

                for (int i=0;i<THREAD_COUNT;i++) {
                    wsInvokers[i].start();
                }
                
                for (int i=0;i<THREAD_COUNT;i++) {
                    try {
                        wsInvokers[i].join();
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
		
		
	}
    
    
    private static class WsInvoker extends Thread {
        
        private Customer customer;
        
        public WsInvoker(Customer c) {
            this.customer = c;
        }
        
        public void run() {
            Customer returnedCustomer = customerManager.updateClient(customer);
            System.out.println(returnedCustomer.getName());
        }
        
    }

}
